import random
import csv
from datetime import datetime, timedelta

# 小商品类别列表
product_categories = [
    "饰品", "玩具", "家居用品", "电子产品", "服装", "箱包", "工艺品",
    "户外用品", "文具", "体育用品", "美容美发工具", "厨房用品"
]

# 主题店铺名称列表
theme_shops = [
    "创意家居馆", "时尚饰品坊", "童趣天地", "电子世界", "潮流服饰店",
    "旅行装备店", "文化礼品店", "运动地带", "美妆小屋", "办公文具店"
]


# 生成随机日期时间
def random_datetime(start_date, end_date):
    time_delta = end_date - start_date
    random_days = random.randint(0, time_delta.days)
    random_seconds = random.randint(0, 86399)  # 一天的秒数
    return start_date + timedelta(days=random_days, seconds=random_seconds)


# 生成随机销售数据
def generate_sales_data(num_records, output_file):
    start_date = datetime(2025, 1, 1)
    end_date = datetime(2025, 6, 30)

    with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
        fieldnames = ['交易ID', '商品类别', '店铺名称', '交易时间', '销售额', '客流量', '客单价']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()

        for i in range(num_records):
            category = random.choice(product_categories)
            shop = random.choice(theme_shops)
            transaction_time = random_datetime(start_date, end_date)
            sales_amount = round(random.uniform(10, 10000), 2)
            customer_flow = random.randint(1, 500)
            customer_price = round(sales_amount / customer_flow, 2) if customer_flow > 0 else 0

            writer.writerow({
                '交易ID': f'TX{i + 1:06d}',
                '商品类别': category,
                '店铺名称': shop,
                '交易时间': transaction_time.strftime('%Y-%m-%d %H:%M:%S'),
                '销售额': sales_amount,
                '客流量': customer_flow,
                '客单价': customer_price
            })


if __name__ == "__main__":
    # 生成10万条模拟数据
    generate_sales_data(10000, 'sales_data.csv')
    print("模拟数据生成完成!")